package com.ljm.sys.dict.controller;

import com.ljm.common.dto.BaseResult;
import com.ljm.common.util.ResultUtil;
import com.ljm.shiro.util.UserUtil;
import com.ljm.sys.dict.api.SysDictItemApi;
import com.ljm.sys.dict.dto.SysDictItemDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
    * 系统字段条目 前端控制器
    * </p>
 *
 * @author HuamingChen
 * @since 2019-02-14
 */
@Api(description="字典条目")
@Controller
@RequestMapping("/dict/sysDictItem")
public class SysDictItemController {

    /**
    * 系统字段条目接口
    */
    @Resource
    private SysDictItemApi sysDictItemApi;

    @ApiOperation(value="保存系统字段条目")
    @PostMapping(value = "/save")
    @ResponseBody
    @RequiresPermissions({"sys:dict:item:add","sys:dict:item:edit"})
    public BaseResult save(@ApiParam(value="字典条目") @RequestBody SysDictItemDto dto, HttpServletRequest request){
        sysDictItemApi.saveData(dto,UserUtil.getUserInfo());
        return ResultUtil.success();
    }

    @ApiOperation(value="删除系统字段条目")
    @PostMapping(value = "/del")
    @ResponseBody
    @RequiresPermissions({"sys:dict:item:del"})
    public BaseResult del(@ApiParam(value="主键") @RequestBody List<Long> ids){
        sysDictItemApi.del(ids);
        return ResultUtil.success();
    }

    @ApiOperation(value="获取系统字段条目")
    @GetMapping(value = "/getById")
    @ResponseBody
    @RequiresPermissions({"sys:dict:item:view","sys:dict:item:edit"})
    public BaseResult getById(@ApiParam(value="主键") @RequestParam Long id){
        return ResultUtil.successData(sysDictItemApi.getDataById(id));
    }

    @ApiOperation(value="分页获取系统字段条目")
    @GetMapping(value = "/getByPage")
    @ResponseBody
    @RequiresPermissions({"sys:dict:item:list"})
    public BaseResult getByPage(
            @ApiParam(value="字典key") @RequestParam(required = false) String dictKey,
            @ApiParam(value="条目key") @RequestParam(required = false) String itemKey,
            @ApiParam(value="条目value") @RequestParam(required = false) String itemValue,
            @ApiParam(value="页码") @RequestParam Integer pageNo,
            @ApiParam(value="页大小") @RequestParam Integer pageSize){
        return ResultUtil.successData(sysDictItemApi.getByPage(dictKey,itemKey,itemValue,pageNo,pageSize));
    }

    @ApiOperation(value="根据字典获取条目")
    @GetMapping(value = "/getItemByDictKey")
    @ResponseBody
    public BaseResult getItemByDictKey(@RequestParam String dictKey){
        return ResultUtil.successData(sysDictItemApi.getItemByDictKey(dictKey));
    }
}
